#!/bin/bash

echo "========================================"
echo "启动项目管理系统前端服务"
echo "========================================"
echo

# 获取脚本所在目录的父目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"

cd "$PROJECT_DIR"

echo "检查Node.js环境..."
if ! command -v node &> /dev/null; then
    echo "错误：未找到Node.js环境，请确保已安装Node.js 16或更高版本"
    exit 1
fi

echo "检查npm环境..."
if ! command -v npm &> /dev/null; then
    echo "错误：未找到npm环境，请确保npm已正确安装"
    exit 1
fi

echo "进入前端目录..."
cd frontend

echo "检查依赖是否已安装..."
if [ ! -d "node_modules" ]; then
    echo "依赖未安装，开始安装依赖..."
    npm install
    if [ $? -ne 0 ]; then
        echo "依赖安装失败，请检查网络连接和npm配置"
        exit 1
    fi
else
    echo "发现node_modules目录，跳过依赖安装"
fi

echo
echo "启动前端开发服务器..."
echo "服务地址：http://localhost:5173"
echo "按 Ctrl+C 停止服务"
echo

npm run dev

echo
echo "前端服务已停止"